// 日志存储
const log4js = require('log4js')

const levels = {
    'trace': log4js.levels.TRACE,
    'debug': log4js.levels.DEBUG,
    'info': log4js.levels.INFO,
    'warn': log4js.levels.WARN,
    'error': log4js.levels.ERROR,
    'fatal': log4js.levels.FATAL
}

log4js.configure({
    appenders: { // 追加器,这里自定义命名为console
        console: { type: 'console' }, // 日志输出到控制台
        info: { type: 'file', filename: 'logs/all-logs.log' }, // 日志输出到文件all-logs.log
        error: {
            type: 'dateFile',
            filename: 'logs/log',
            pattern: 'yyyy-MM-dd.log',
            alwaysIncludePattern: true // 设置文件名为 filename + pattern的组合名
        }
    },
    categories: { // 日志分类 default,info,error三类
        default: {
            appenders: ['console'],
            level: 'debug'
        },
        info: {
            appenders: ['console', 'info'],
            level: 'info'
        },
        error: {
            appenders: ['console', 'error'],
            level: 'error'
        }
    }
})

// 导出debug这个函数，用于debug日志输出
exports.debug = content => {
    let logger = log4js.getLogger('debug') // 获取logger实例
    logger.level = levels.debug // 定义logger级别
    logger.debug(content) // 输出logger日志
}

exports.error = content => {
    let logger = log4js.getLogger('error')
    logger.level = levels.error
    logger.error(content)
}

exports.info = content => {
    let logger = log4js.getLogger('info')
    logger.level = levels.info
    logger.info(content)
}